#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

# SVN_OPT="-r 55"
SVN_OPT="-r 51"

export PATH=$TOP_DIR/cross/bin:$PATH

# export MAKE_OPT="-j 2"

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -d xavs ] ; then
		svn co $SVN_OPT http://xavs.svn.sourceforge.net/svnroot/xavs/trunk xavs
	else
		cd xavs
		svn up $SVN_OPT
		cd ..
	fi
	cd ..
fi


if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32 --cross-prefix=x86_64-w64-mingw32-"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32 --cross-prefix=i686-w64-mingw32-"
	fi
	rm -rf $BIN_DIR/xavs
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_xavs
	cp -R $SRC_DIR/xavs bd_xavs
	cd bd_xavs
# patch -p0 < $TOP_DIR/patch/xavs-configure-w64.patch
	./configure --prefix=$BIN_DIR/xavs $HOST_FLAG --disable-shared 

# --enable-asm

	make $MAKE_OPT
	make install
# rm $BIN_DIR/xavs/lib/libxavs.a
	cd ..
	cd ..
fi

